package example;

import java.io.*;
import java.util.Scanner;

//实现复制文件,把文件每一个字节读出来,写入另一个文件中
public class demo18 {

    public static void main(String[] args) {
        //1.输入路径,并且做校验.
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入源文件的路径:");
        String srcPath = scanner.next();
        File srcFile = new File(srcPath);

        if(!srcFile.isFile()){
            System.out.println("源文件路径有误!");
            return;
        }
        System.out.println("请输入目标文件的路径:");
        String destPath = scanner.next();
        File destFile = new File(destPath);
        if(!destFile.getParentFile().isDirectory()){

            System.out.println("目标文件的路径是否有误!");
            return;

        }
        //2.执行复制的过程
        try(InputStream inputStream = new FileInputStream(srcFile);
            OutputStream outputStream =new FileOutputStream(destFile)) {
            while (true){
                byte[] buffer = new byte[1024];
                int n = inputStream.read(buffer);
                System.out.println("n=" + n);
                if (n == -1) {
                    break;
                }
                //需要把buffer写入到outputstream
                outputStream.write(buffer,0,n);

            }


        }catch (IOException e){
            e.printStackTrace();
        }
    }
}
//此处代码逻辑,效率很低,每次查询,都会涉及大量硬盘IO操作
//搜索引擎这样的场景,不能通过上述"遍历文件"方式实现
